package com.explorati.LinkedList.LeetCode203;

/**
 * Definition for singly-linked list. public class ListNode { int val; ListNode
 * next; ListNode(int x) { val = x; } }
 */
class Solution {
	public ListNode removeElements(ListNode head, int val) {

		while (head != null && head.val == val) {
			ListNode node = head;
			head = head.next;
			node.next = null;
		}
		if (head == null) {
			return null;
		}
		ListNode prev = head;
		while (prev.next != null) {
			if (prev.next.val == val) {
				ListNode node = prev.next;
				prev.next = node.next;
				node.next = null;
			} else {
				prev = prev.next;
			}
		}
		return head;

		// if (head == null) {
		// return null;
		// }
		// while (head.val != val) {
		// ListNode node = head;
		// head = head.next;
		// node.next = null;
		// }
		// while (head != null) {
		// if (head.next.val == val) {
		// ListNode node = head.next;
		// head.next = node.next;
		// node.next = null;
		// } else {
		// head = head.next;
		// }
		// }
		// return head;
	}
}
